package com.ruoyi.purchase.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.purchase.mapper.PurchaseOrderMapper;
import com.ruoyi.purchase.mapper.PurchaseOrderMaterialMapper;
import com.ruoyi.purchase.pojo.PurchaseApplyMaterial;
import com.ruoyi.purchase.pojo.PurchaseOrder;
import com.ruoyi.purchase.pojo.PurchaseOrderMaterial;
import com.ruoyi.purchase.service.PurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购订单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-25
 */
@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService
{
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Autowired
    private PurchaseOrderMaterialMapper purchaseOrderMaterialMapper;
    /**
     * 查询采购订单
     *
     * @param id 采购订单主键
     * @return 采购订单
     */
    @Override
    public PurchaseOrder selectPurchaseOrderById(Integer id)
    {
        return purchaseOrderMapper.selectPurchaseOrderById(id);
    }

    /**
     * 查询采购订单列表
     *
     * @param purchaseOrder 采购订单
     * @return 采购订单
     */
    @Override
    public List<PurchaseOrder> selectPurchaseOrderList(PurchaseOrder purchaseOrder)
    {
        return purchaseOrderMapper.selectPurchaseOrderList(purchaseOrder);
    }

    /**
     * 新增采购订单
     *
     * @param purchaseOrder 采购订单
     * @return 结果
     */
    @Transactional
    @Override
    public void insertPurchaseOrder(PurchaseOrder purchaseOrder)
    {
        purchaseOrder.setCreateTime(DateUtils.getNowDate());
        purchaseOrderMapper.insertPurchaseOrder(purchaseOrder);
        for (PurchaseOrderMaterial purchaseOrderMaterial : purchaseOrder.getPurchaseOrderMaterials()){
            purchaseOrderMaterial.setPurchaseOrderId(purchaseOrder.getId());
            purchaseOrderMaterialMapper.insertPurchaseOrderMaterial(purchaseOrderMaterial);
        }
    }

    @Transactional
    @Override
    public int updateStatus(PurchaseOrder purchaseOrder) {

        purchaseOrder.setUpdateTime(DateUtils.getNowDate());
        return purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
    }

    /**
     * 修改采购订单
     *
     * @param purchaseOrder 采购订单
     * @return 结果
     */
    @Override
    public int updatePurchaseOrder(PurchaseOrder purchaseOrder)
    {
        for (PurchaseOrderMaterial purchaseOrderMaterial : purchaseOrder.getPurchaseOrderMaterials()){
            if (purchaseOrderMaterial.getId() != null){
                purchaseOrderMaterial.setPurchaseOrderId(purchaseOrder.getId());
                purchaseOrderMaterialMapper.updatePurchaseOrderMaterial(purchaseOrderMaterial);
            }else {
                purchaseOrderMaterial.setPurchaseOrderId(purchaseOrder.getId());
                purchaseOrderMaterialMapper.insertPurchaseOrderMaterial(purchaseOrderMaterial);
            }
        }
        purchaseOrder.setUpdateTime(DateUtils.getNowDate());
        return purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
    }

    /**
     * 批量删除采购订单
     *
     * @param ids 需要删除的采购订单主键
     * @return 结果
     */
    @Override
    public int deletePurchaseOrderByIds(Integer[] ids)
    {
        return purchaseOrderMapper.deletePurchaseOrderByIds(ids);
    }

    /**
     * 删除采购订单信息
     *
     * @param id 采购订单主键
     * @return 结果
     */
    @Override
    public int deletePurchaseOrderById(Integer id)
    {
        return purchaseOrderMapper.deletePurchaseOrderById(id);
    }
}
